package com.dst.smart.dao.impl.factory;

import java.util.ArrayList;
import java.util.List;
import com.dst.smart.app.MyApplication;
import com.dst.smart.model.Device;

public class DeviceFactory {
	public static List<Device> list = new ArrayList<Device>();

	static {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
	}

	public static List<Device> getAll() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		return list;
	}

//	public static List<Device> getMode() {
//		List<Device> resultList = new ArrayList<Device>();
//		for (Device _device : list) {
//			if (_device.getDeviceTypeId() == 6) {
//				resultList.add(_device);
//			}
//		}
//		return resultList;
//	}

	public static int getCount() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		return list.size();
	}

	public static List<Device> getDevices(int roomId) {
		List<Device> resultList = new ArrayList<Device>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}

		for (Device device : list) {
			if (device.getRoomId() == roomId) {
				resultList.add(device);
			}
		}
		return resultList;
	}

	public static List<Device> getDeviceByName(String deviceName) {
		List<Device> resultList = new ArrayList<Device>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}

		for (Device device : list) {
			if (device.getDeviceName().equals(deviceName)) {
				resultList.add(device);
			}
		}
		return resultList;
	}

	public static List<Device> getDevices(String deviceID) {
		List<Device> resultList = new ArrayList<Device>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}

		for (Device device : list) {
			if (device.getDeviceID().equals(deviceID)) {
				resultList.add(device);
			}
		}
		return resultList;
	}

	public static String getName(String deviceID) {
		String result = "";
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}

		for (Device device : list) {
			if (device.getDeviceID().equals(deviceID)) {
				result = device.getDeviceName();
				break;
			}
		}
		return result;
	}

//	public static Device getDevice() {
//		if (list.size() == 0) {
//			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
//		}
//		if (list.size() > 0) {
//			return list.get(0);
//		} else {
//			Device device = new Device();
//
//			device.setId(0);
//			device.setDeviceNickName("");
//
//			device.setSeqencing(0);
//			device.setDevice_onLine(0);
//			device.setDeviceTimer("");
//			device.setDeviceOrdered("");
//			device.setStartTime("");
//			device.setEndTime("00:00");
//			device.setSceneId(0);
//			device.setDeviceType_key(0);
//			device.setDeviceType_Language(0);
//			device.setDeviceType_click(0);
//			device.setDeviceName("");
//			device.setDeviceType_Name("");
//			device.setProducts_name("");
//			device.setDeviceType("00");
//			device.setDeviceID("000000");
//			device.setDeviceImage(null);
//			device.setDeviceIO("0");
//			device.setStartTime("00:00");
//			device.setCreationTime("");
//			device.setEndTime("00:00");
//			device.setDeviceGrading(0);
//			device.setDeviceArce(0);
//			device.setDeviceTypeId(0);
//			device.setDeviceProductId(0);
//			device.setDeviceProductIo(0);
//			device.setActionShow(0);
//			device.setFunctionShow(0);
//			device.setActionPramShow(0);
//			device.setFunctionParamShow(0);
//			return device;
//		}
//	}

	public static void delete(int id) {
		if (list.size() > 0) {
			for (Device device : list) {
				if (device.getId() == id) {
					list.remove(device);
					break;
				}
			}
		}
	}

	public static void add(Device device) {
		if (list == null) {
			list = new ArrayList<Device>();
		}
		list.add(device);
	}
	public static String judgeName(int roomId,String deviceName) {
		String result = "";
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		List<Device> devicesArr=DeviceFactory.getDevices(roomId);
		for (Device device : devicesArr) {
			result = device.getDeviceName();
			if (result.indexOf(deviceName) != -1
					|| deviceName.indexOf(result) != -1) {
				break;
			}
		}
		return result;
	}

	public static int judgeName1(String deviceName) {
		int result = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		for (Device device : list) {
			String name = device.getDeviceName();
			if (name.indexOf(deviceName) != -1
					|| deviceName.indexOf(name) != -1) {
				result = 1;
				break;
			}
		}
		return result;
	}

	public static int updateName(int id, String deviceName, String oldName) {
		int result = 0;

		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		if (deviceName.equals(oldName)) {
			result = 0;
			return result;
		}

		int num = 0;
		for (Device device : list) {
			String name = device.getDeviceName();
			if (name.indexOf(deviceName) != -1
					|| deviceName.indexOf(name) != -1) {
				if (device.getId() == id) {
					result = 0;
				} else {
					num++;
				}
				if (num > 0) {
					result = 1;
					break;
				}
			}
		}
		return result;
	}

//	public static List<Device> getDeviceByAction(int arceId, int typeId,
//			int yyId) {
//
//		List<Device> resultList = new ArrayList<Device>();
//		if (list.size() == 0) {
//			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
//		}
//		if (arceId > 0 && typeId > 0) {
//
//			for (Device device : list) {
//				if (device.getDeviceArce() == arceId
//						&& device.getDeviceTypeId() == typeId) {
//					resultList.add(device);
//				}
//			}
//
//		} else if (arceId > 0 && typeId <= 0) {
//			for (Device device : list) {
//				if (device.getDeviceArce() == arceId) {
//					resultList.add(device);
//				}
//			}
//		} else if (arceId <= 0 && typeId > 0) {
//
//			for (Device device : list) {
//				if (device.getDeviceTypeId() == typeId) {
//					resultList.add(device);
//				}
//			}
//		} else if (arceId <= 0 && typeId <= 0) {
//			resultList.addAll(list);
//		}
//		return resultList;
//	}
//
//	public static Device getModeDevice() {
//		Device device = null;
//		for (Device device2 : list) {
//			if (device2.getDeviceArce() == 0) {
//				device = device2;
//				break;
//			}
//		}
//		return device;
//	}

	public static void clearList() {
		list = MyApplication.getInstance().getWidgetDataBase().getDevices();
	}

	public static List<Device> getDeviceByAction(int roomId, int deviceTypeKey,
												 int pastControl, int isShow) {
		List<Device> resultList = new ArrayList<Device>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getDevices();
		}
		if (roomId > 0 && deviceTypeKey > 0) {
			for (Device device : list) {
				if (device.getRoomId() == roomId
						&& device.getDeviceTypeKey() == deviceTypeKey&&device.getIsShow()==isShow) {
					resultList.add(device);
				}
			}

		} else if (roomId > 0 && deviceTypeKey <= 0) {
			for (Device device : list) {
				if (device.getRoomId() == roomId&&device.getIsShow()==isShow) {
					resultList.add(device);
				}
			}
		} else if (roomId <= 0 && deviceTypeKey > 0) {
			for (Device device : list) {
				if (device.getDeviceTypeKey() == deviceTypeKey&&device.getIsShow()==isShow) {
					resultList.add(device);
				}
			}
		} else if (roomId <= 0 && deviceTypeKey <= 0) {
			for (Device device : list) {
				if(pastControl==1){
					if ((pastControl==0&&device.getRoomId()==0)) {
						continue;
					}
					resultList.add(device);
				}
				else{
					if(device.getIsShow()==isShow){
						resultList.add(device);
					}
				}
			}
		}


		return resultList;
	}
}
